package com.deliveryservicesystem;

import com.deliveryservicesystem.dataaccess.DatabaseConnection;
import com.deliveryservicesystem.service.ScriptRunner;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.*;

/**
 * File Name: InitDatabase.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.test
 * Author: Lance
 * Date: Jun 19, 2012
 * Editor:
 */
public class InitDatabase
{

    public InitDatabase()
    {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {
    }

    @AfterClass
    public static void tearDownClass() throws Exception
    {
    }

    @Before
    public void setUp()
    {
    }

    @After
    public void tearDown()
    {
    }

    @Test
    public void testInitDatabase()
    {
        System.out.println("Database URL = jdbc:oracle:thin:@//127.0.0.1:1521/XE\n"
                + "User name = hr\n"
                + "Password = hr\n");
        System.out.flush();

        try
        {
            DatabaseConnection databaseConnsection = DatabaseConnection.getInstance();
            runScript(databaseConnsection.getConnection());
        }
        catch (SQLException ex)
        {
            System.err.println(ex.toString());
        }
    }

    private void runScript(Connection connection) throws SQLException
    {
        ScriptRunner scriptRunner = new ScriptRunner(connection, false, false);
        scriptRunner.setDelimiter("/", false);
        Reader reader;
        try
        {
            scriptRunner.setLogWriter(new PrintWriter(new FileOutputStream("ScriptLog.txt", true)));
            scriptRunner.setErrorLogWriter(new PrintWriter(new FileOutputStream("ScriptErrorLog.txt", true)));
            reader = new FileReader("./src/com/deliveryservicesystem/app/conf/ALL_DELIMITED.sql");
            scriptRunner.runScript(reader);
        }
        catch (IOException ex)
        {
            System.err.println(ex.toString());
        }
    }
}